package com.share.zabbix.method;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.share.zabbix.request.ZabbixRequest;
import com.share.zabbix.request.ZabbixRequestBuilder;
import com.share.zabbix.response.ZabbixResponseTrigger;

public class ZabbixTriggerMethod extends ZabbixBaseMethod{
	
	public static Object getTrigger(String triggerId) {
		ZabbixRequest request = ZabbixRequestBuilder.newBuilder().method("trigger.get")
				.extendOutput()
				.paramEntry("triggerids", triggerId)
				.build();
		try {
			JSONObject response = call(request);
			Gson gson = new GsonBuilder().create();
			List<ZabbixResponseTrigger> hisls = gson.fromJson(response.getJSONArray(ZABBIX_KEY_RESULT).toString(),
					new TypeToken<ArrayList<ZabbixResponseTrigger>>() {
					}.getType());

			return hisls;
		} catch (Exception e) {
			log.error(e);
			log.info("Zabbix查询trigger失败！");
		}
		return null;
	}
	
	public static Object getWarnningTrigger() {
		ZabbixRequest request = ZabbixRequestBuilder.newBuilder().method("trigger.get")
				.extendOutput()
				.filterEntry("value", 1)
				.paramEntry("selectHosts", Arrays.asList("name", "host"))
				.paramEntry("selectItems", Arrays.asList("name", "lastvalue", "units"))
				.sortByDesc("priority")
				.build();
		try {
			JSONObject response = call(request);
			Gson gson = new GsonBuilder().create();
			List<ZabbixResponseTrigger> hisls = gson.fromJson(response.getJSONArray(ZABBIX_KEY_RESULT).toString(),
					new TypeToken<ArrayList<ZabbixResponseTrigger>>() {
					}.getType());

			return hisls;
		} catch (Exception e) {
			log.error(e);
			log.info("Zabbix查询trigger失败！");
		}
		return null;
	}
}
